<?php

namespace JsonLowCode\Support;

use Closure;
use JsonLowCode\JsBase;
use JsonLowCode\Schema;

/**
 * JsMessage
 * @method static JsMessage make(bool $isFunction = true)
 */
class JsMessage extends JsBase
{
    protected string $message = "window.\$message";

    protected array $options = [];

    /**
     * @param bool $isFunction 是否函数包裹
     */
    public function __construct(bool $isFunction = true){
        $this->message = Js::message();
        $this->setJsFunction($isFunction);
    }

    /**
     * 是否显示 close 图标
     * @param bool $bool
     * @return $this
     */
    public function closable(bool $bool = false): static
    {
        $this->options['closable'] = $bool;
        return $this;
    }

    /**
     * 信息展示的时长 单位毫秒 1000毫秒=1秒
     * @param int $duration
     * @return $this
     */
    public function duration(int $duration = 1500): static
    {
        $this->options['duration'] = $duration;
        return $this;
    }

    /**
     * 是否展示图标
     * @param bool $bool
     * @return $this
     */
    public function showIcon(bool $bool = false): static
    {
        $this->options['showIcon'] = $bool;
        return $this;
    }

    /**
     * Hover 到信息上是否不销毁
     * @param bool $bool
     * @return $this
     */
    public function keepAliveOnHover(bool $bool = false): static
    {
        $this->options['keepAliveOnHover'] = $bool;
        return $this;
    }

    /**
     * 信息类型
     * @param string $type 'info' | 'success' | 'warning' | 'error' | 'loading' | 'default'
     * @return $this
     */
    public function type(string $type = 'info' | 'success' | 'warning' | 'error' | 'loading' | 'default'): static
    {
        $this->options['type'] = $type;
        return $this;
    }

    /**
     * 信息图标
     * @param Schema $icon
     * @param array<int,string> $params
     * @return JsMessage
     */
    public function icon(Schema $icon, array $params = []): static
    {
        $this->options['icon'] = JsParse::JSSlot($icon, $params);
        return $this;
    }

    /**
     * 信息消失动画结束的回调
     * @param string $event
     * @return $this
     */
    public function onAfterLeave(string $event): static
    {
        $this->options['onAfterLeave'] = scriptFunc($event);
        return $this;
    }

    /**
     * 点击关闭图标的回调
     * @param string $event
     * @return $this
     */
    public function onClose(string $event): static
    {
        $this->options['onClose'] = scriptFunc($event);
        return $this;
    }

    /**
     * 信息开始消失的回调
     * @param string $event
     * @return $this
     */
    public function onLeave(string $event): static
    {
        $this->options['onLeave'] = scriptFunc($event);
        return $this;
    }

    /**
     * 查看当前$message
     */
    public function onView(): string
    {
        $this->script = "console.log({$this->message}, this)";
        return $this->toString();
    }

    /**
     * 销毁所有弹出的信息
     * @return string
     */
    public function destroyAll(): string
    {
        $this->script = "{$this->message}.destroyAll()";
        return $this->toString();
    }

    /**
     * 调用 success 类型的信息
     * @param string $content
     * @param Closure|null $closure
     * @return string
     */
    public function success(string $content, Closure $closure= null): string
    {
        return $this->makeScript("success", $content, $closure);
    }

    /**
     * 调用 error 类型的信息
     * @param string $content
     * @param Closure|null $closure
     * @return string
     */
    public function error(string $content, Closure $closure= null): string
    {
        return $this->makeScript("error", $content, $closure);
    }

    /**
     * 调用 info 类型的信息
     * @param string $content
     * @param Closure|null $closure
     * @return string
     */
    public function info(string $content, Closure $closure= null): string
    {
        return $this->makeScript("info",$content, $closure);
    }

    /**
     * 调用 warning 类型的信息
     * @param string $content
     * @param Closure|null $closure
     * @return string
     */
    public function warning(string $content, Closure $closure= null): string
    {
        return $this->makeScript("warning",$content, $closure);
    }

    /**
     * 调用 loading 类型的信息
     * @param string $content
     * @param Closure|null $closure
     * @return string
     */
    public function loading(string $content, Closure $closure= null): string
    {
        return $this->makeScript("loading", $content, $closure);
    }

    /**
     * 调用 create 类型的信息
     * @param string $content
     * @param Closure|null $closure
     * @return string
     */
    public function create(string $content, Closure $closure= null): string
    {
        return $this->makeScript("create", $content, $closure);
    }

    /**
     * @param string $type
     * @param string $content
     * @param Closure|null $closure
     * @return string
     */
    protected function makeScript(string $type = 'info' | 'success' | 'warning' | 'error' | 'loading' | 'default' | 'create', string $content = "", Closure $closure= null): string
    {
        $content = $this->jsFrom($content);
        $type = $type=='default'?'create':$type;
        $options = $this->options;
        if ($options){
            $options = $this->jsFrom($options);
            $this->script = "{$this->message}.{$type}({$content},{$options})";
        }else{
            $this->script = "{$this->message}.{$type}({$content})";
        }
        return $this->callback($closure)->toString();
    }
}
